package com.Sdebruijn.curriculumvitae.swipey;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;

import com.Sdebruijn.curriculumvitae.R;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.slidingmenu.lib.SlidingMenu;
import com.slidingmenu.lib.app.SlidingFragmentActivity;

public abstract class BaseSwipeyTabsActivity extends SlidingFragmentActivity implements
		OnPageChangeListener {

	public static final String EXTRA_INDEX = "com.Sdebruijn.curriculumvitae.INDEX";

	public static final int PAGE_CACHE_LIMIT = 2;

	private int mSelectedTab = 0;

	/** The pager. */
	protected ViewPager mPager;

	/** The swipey tabs. */
	protected SwipeyTabs mSwipeyTabs;

	/** The swipey tabs adapter. */
	protected SwipeyTabsAdapter mSwipeyTabsAdapter;

	/** The pager adapter. */
	protected SwipeyPagerAdapter mPagerAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wrapper_swipey_screen);

		mPager = (ViewPager) findViewById(R.id.pager);
		mPagerAdapter = new SwipeyPagerAdapter(getSupportFragmentManager(), this);
		mSwipeyTabs = (SwipeyTabs) findViewById(R.id.swipey_tabs);

		mPager.setOnPageChangeListener(this);
		mPager.setPageMargin(1);
		mPager.setOffscreenPageLimit(PAGE_CACHE_LIMIT);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt("index", getSupportActionBar().getSelectedNavigationIndex());
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageScrollStateChanged(int)
	 */
	@Override
	public void onPageScrollStateChanged(int state) {
		mSwipeyTabs.onPageScrollStateChanged(state);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageScrolled(int,
	 *      float, int)
	 */
	@Override
	public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
		mSwipeyTabs.onPageScrolled(position, positionOffset, positionOffsetPixels);
	}

	/**
	 * Set the selected tab and invalidate the options menu so it is rebuilt for
	 * the selected tab.
	 * 
	 * @see #onPrepareOptionsMenu for further details.
	 * @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageSelected(int)
	 */
	@Override
	public void onPageSelected(int position) {
		switch (position) {
		case 0:
			getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
			break;
		default:
			getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
			break;
		}

		mSelectedTab = position;
		invalidateOptionsMenu();
		// We have to tell the tabs adapter we're changing pages or the tab
		// indicator won't update.
		// Due to us overriding onPageSelected here in the activity.
		mSwipeyTabs.onPageSelected(position);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.actionbarsherlock.app.SherlockFragmentActivity#onCreateOptionsMenu(com.actionbarsherlock.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			toggle();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * Handling this menu creation in the parent activity of a fragment is a
	 * workaround for a rare condition issue between the Tabs Pager and the
	 * ActionBar. In addition to this method, onCreateOptionsMenu is overridden
	 * as well as implementing
	 * android.support.v4.view.ViewPager.OnPageChangeListener to use the
	 * onPageSelected method.
	 * 
	 * @see<a 
	 *        href="https://github.com/jakewharton/actionbarsherlock/issues/272">
	 *        ABS #272</a> for details.
	 * @see<a 
	 *        href="https://github.com/JakeWharton/ActionBarSherlock/issues/476">
	 *        ABS #476</a> for workaround clues.
	 * @see com.actionbarsherlock.app.SherlockFragmentActivity#onPrepareOptionsMenu(com.actionbarsherlock.view.Menu)
	 * @see #onCreateOptionsMenu(Menu)
	 * @see #onPageSelected
	 */
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		super.onPrepareOptionsMenu(menu);

		menu.clear();

		addActionBarMenu(menu, mSelectedTab);

		return true;
	}

	protected abstract boolean addActionBarMenu(Menu menu, int position);

}